{include file="common/header"}
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <table id="dataTable" lay-filter="dataTable"></table>
    </div>
</div>

<script type="text/html" id="toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-md" lay-event="recycle">
        <i class="layui-icon layui-icon-delete"></i>
        回收站
    </button>
</script>

<script type="text/html" id="options">
    <button class="pear-btn pear-btn-primary pear-btn-xs" lay-event="edit">
        <i class="layui-icon">编辑</i>
    </button>
    <button class="pear-btn pear-btn-xs" lay-event="more">
        <i class="layui-icon">更多</i>
    </button>
</script>

{include file="common/footer"}
<script>
    layui.use(['table', 'form', 'jquery', 'common', 'dropdown'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let dropdown = layui.dropdown;

        let MODULE_PATH = "{$Request.root}/role/";

        let cols = [
            [
                {
                    field: 'id',
                    title: 'ID',
                    unresize: true,
                    align: 'center',
                    width: 80
                }, {
                field: 'name',
                title: '角色名称',
                unresize: true,
                align: 'center'
            }, {
                field: 'desc',
                title: '描述',
                unresize: true,
                align: 'center'
            },
                {
                    field: 'create_time',
                    title: '创建时间',
                    unresize: true,
                    align: 'center'
                },
                {
                    field: 'update_time',
                    title: '更新时间',
                    unresize: true,
                    align: 'center'
                },
                {
                    title: '操作',
                    toolbar: '#options',
                    unresize: true,
                    align: 'center',
                    width: 130
                }
            ]
        ]

        table.render({
            elem: '#dataTable',
            url: MODULE_PATH + 'index',
            page: true,
            cols: cols,
            cellMinWidth: 100,
            skin: 'grid',
            even: 'true',
            size: 'lg',
            toolbar: '#toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });

        table.on('tool(dataTable)', function (obj) {
            if (obj.event === 'edit') {
                window.edit(obj);
            } else if (obj.event === 'more') {
                dropdown.render({
                    elem: this, // 触发事件的 DOM 对象
                    show: true, // 外部事件触发即显示
                    data: [{
                        title: '角色权限',
                        id: 'permission'
                    }, {
                        title: '删除角色',
                        id: 'remove'
                    }],
                    click: function (menudata) {
                        if (menudata.id === 'permission') {
                            window.permission(obj);
                        } else if (menudata.id === 'remove') {
                            window.remove(obj);
                        }
                    },
                    align: 'right', // 右对齐弹出
                    style: 'box-shadow: 1px 1px 10px rgb(0 0 0 / 12%);' // 设置额外样式
                })
            }
        });

        table.on('toolbar(dataTable)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'recycle') {
                window.recycle(obj);
            }
        });

        window.add = function () {
            layer.open({
                type: 2,
                title: '新增角色',
                offset: 'r',
                anim: 'slideLeft', // 从右往左
                shade: 0.1,
                shadeClose: true,
                area: common.screenSide(),
                content: MODULE_PATH + 'add'
            });
        }

        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '修改角色',
                offset: 'r',
                anim: 'slideLeft', // 从右往左
                shade: 0.1,
                shadeClose: true,
                area: common.screenSide(),
                content: MODULE_PATH + 'edit/id/' + obj.data['id']
            });
        }

        window.permission = function (obj) {
            layer.open({
                type: 2,
                title: '分配权限',
                offset: 'r',
                anim: 'slideLeft', // 从右往左
                shade: 0.1,
                shadeClose: true,
                area: common.screenSide(),
                content: MODULE_PATH + 'permission/id/' + obj.data['id']
            });
        }

        window.recycle = function () {
            layer.open({
                type: 2,
                title: '回收站',
                offset: 'r',
                anim: 'slideLeft', // 从右往左
                shade: 0.1,
                shadeClose: true,
                area: common.screenSide(),
                content: MODULE_PATH + 'recycle',
                cancel: function () {
                    table.reload('dataTable');
                }
            });
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该角色', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: MODULE_PATH + 'remove',
                    data: {id: obj.data['id']},
                    dataType: 'json',
                    type: 'POST',
                    success: function (res) {
                        layer.close(loading);
                        //判断有没有权限
                        if (res && res.code == 999) {
                            layer.msg(res.msg, {
                                icon: 5,
                                time: 2000,
                            })
                            return false;
                        } else if (res.code == 200) {
                            layer.msg(res.msg, {
                                icon: 1,
                                time: 1000
                            }, function () {
                                obj.del();
                            });
                        } else {
                            layer.msg(res.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.refresh = function (param) {
            table.reload('dataTable');
        }
    })
</script>
</body>
</html>
